/**
 * Created by caosh on 2018/5/7 0007.
 */
var V$login = new Vue({
    el: '#login_page',
    data: {
        loginName: '',
        password: '',
        user: {},
        isFirstLogin: false,
        modify: {
            loginName: '',
            oldPwd: '',
            newPwd: ''
        },
        confirmPwd: '',
        valid: true
    },
    methods: {
        login: function (e) {

            if (this.loginName == '') {
                console.log('请输入用户名');
                this.changeStatus(['loginNameClassObj'], 'isError');
                return;
            } else if (this.password == '') {
                console.log('请输入密码');
                this.changeStatus(['passwordClassObj'], 'isError');
                return;
            }

            // 缓存用户信息，当token过期时从缓存中取最后的用户信息获取token
            Vue.saveStore('userinfo', {'loginName': this.loginName, 'password': this.password});
            this.doLogin();
        },
        doLogin: function() {
            var _this = this;
            _this.setAjaxOptions('/login', {'loginName': this.loginName, 'password': this.password}, function (result, status, jqXHR) {
                // 登录成功
                if (result.retCode == 0) {
                    console.log('登录成功！');
                    _this.user = result.data;
                    Vue.saveStore('user', _this.user);
                    Vue.saveToken({'accessToken': _this.user.token, 'expiresIn': _this.user.expiresIn});
                    if (_this.user.firstLogin == 0) {
                        _this.isFirstLogin = true;
                    } else {
                        window.location.href = './survey/index.html';
                    }
                } else { // 登录失败
                    console.log(result.errMsg);
                    _this.changeStatus('loginNameClassObj', 'isError');
                }
            });
            _this.doAjax();
        },
        editPwd: function () {
            var _this = this;
            _this.checkUser();
            if (!_this.valid) {
                return;
            }

            _this.setAjaxOptions('/editPwd', _this.modify, function (result, status, jqXHR) {
                if (result.retCode == 0) {

                    window.location.href = './survey/index.html';
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        checkUser: function () {
            if (this.modify.loginName != this.loginName) {
                this.valid = false;
            }

            if (this.modify.newPwd.length<6) {
                this.valid = false;
            }

            if (this.confirmPwd != this.modify.newPwd) {
                this.valid = false;
            }
        },
        resetClassObj: function () {
            this.changeStatus(['loginNameClassObj'], 'isDefault');
            this.changeStatus(['passwordClassObj'], 'isDefault');
            this.changeStatus(['userClassObj'], 'isDefault');
        }
    },
    mixins: [StatusMixin, AjaxMixin, UserMixin],
    mounted: function () {
        Vue.useLayerElement();
        this.resetClassObj();
        Vue.useLayerForm();
    },
    watch: {
        'modify.newPwd': function (val, oldVal) {
            if (val.length>20) {
                this.user.newPwd = val.substring(0, 20);
            }
        },
        'confirmPwd': function (val, oldVal) {
            if (val.length>20) {
                this.confirmPwd = val.substring(0, 20);
            }
        },
        'isFirstLogin': function (val, oldVal) {

        }
    }
});
